package com.wf.LeetCode;

import com.wf.atm.service.Bank;

import java.util.*;

/**
 * @author mpk
 * @since 2024/6/10 18:33:40
 */
public class IsValid {
    public static void main(String[] args) {


        String s = "()[]{}";
        String s1 = "(](";
        System.out.println(isValid(s));
        System.out.println(isValid(s1));
        System.out.println(isValid1(s));
        System.out.println(isValid1(s1));

    }

    public static boolean isValid(String s) {

        boolean flag = true;


        while (s.contains("()")||s.contains("[]")||s.contains("{}")) { //

            s = s.replace("()", "");
            s = s.replace("[]", "");
            s = s.replace("{}", "");
        }

        if (s.contains("{") || s.contains("}") || s.contains("(") || s.contains(")") || s.contains("[") || s.contains("]")) {
            flag = false;
        }

        return flag;

        }
    public static boolean isValid1(String s) {
        Stack<Character> stack = new Stack<>();


        for (char c : s.toCharArray()) {
            if (c == '(' || c == '[' || c == '{') {
                stack.push(c);
            } else if (c == ')' || c == ']' || c == '}') {
                if (stack.isEmpty()) {
                    return false; // 无法匹配，提前返回false
                }

                char top = stack.pop();
                if ((c == ')' && top != '(') || (c == ']' && top != '[') || (c == '}' && top != '{')) {
                    return false; // 括号类型不匹配，提前返回false
                }
            }
        }

        return stack.isEmpty(); // 如果栈为空，表示所有括号都匹配成功
    }

}
